// pages/detail/detail.js
// pages/detail/detail.js
const db=wx.cloud.database()
const photos=db.collection('photos')
Page({
  previewPhoto:function(){
var urls=[this.data.photo.photoUrl]
wx.previewImage({
  urls:urls,
})
wx.cloud.deletePhoto({
  delete: function (event) {
    console.log(event)
    db.collection('photos').doc(this.data.id).remove({
      success: function (res) {
        console.log(res)
      }
    })
  }
})


},
gotoreplay: function(){ 
  wx.navigateTo({ url: '../replay/replay' }) },
  onLoad: function(options) {

    that = this;
    that.data.id = options.id;
    that.data.openid = options.openid;
    // 获取话题信息
    db.collection('topic').doc(that.data.id).get({
      success: function(res) {
        that.topic = res.data;
        that.setData({
          topic: that.topic,
        })
      }
    })

    // 获取收藏情况
    db.collection('collect')
      .where({
        _openid: that.data.openid,
        _id: that.data.id

      })
      .get({
        success: function(res) {
          if (res.data.length > 0) {
            that.refreshLikeIcon(true)
          } else {
            that.refreshLikeIcon(false)
          }
        },
        fail: console.error
      })
  },
  onShow: function() {
    // 获取回复列表
    that.getReplay()
  },
  getReplay: function() {
    // 获取回复列表
    db.collection('replay')
      .where({
        t_id: that.data.id
      })
      .get({
        success: function(res) {
          // res.data 包含该记录的数据
          console.log(res)
          that.setData({
            replays: res.data
          })
        },
        fail: console.error
      })
  },
/**
   * 刷新点赞icon
   */
  refreshLikeIcon(isLike) {
    that.data.isLike = isLike
    that.setData({
      isLike: isLike,
    })
  },
  // 预览图片
  previewImg: function(e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;

    wx.previewImage({
      //当前显示图片
      current: this.data.topic.images[index],
      //所有图片
      urls: this.data.topic.images
    })
  },
  /**
   * 喜欢点击
   */
  onLikeClick: function(event) {
    console.log(that.data.isLike);
    if (that.data.isLike) {
      // 需要判断是否存在
      that.removeFromCollectServer();
    } else {
      that.saveToCollectServer();
    }
  },
  /**
   * 添加到收藏集合中
   */
  saveToCollectServer: function(event) {
    db.collection('collect').add({
      // data 字段表示需新增的 JSON 数据
      data: {
        _id: that.data.id,
        date: new Date(),
      },
      success: function(res) {
        that.refreshLikeIcon(true)
        console.log(res)
      },
    })
  },
  /**
   * 从收藏集合中移除
   */
  removeFromCollectServer: function(event) {
    db.collection('collect').doc(that.data.id).remove({

      success: that.refreshLikeIcon(false),
    });
  },


// 自定义函数--下载图片到本地
downloadPhoto:function(){
  //从云储存中进行图片下载
  wx.cloud.downloadFile({
    fileID:this.data.photo.photoUrl,
    success:res=>{
      //保存图片到本地相册
      wx.saveImageToPhotosAlbum({
        filePath:res.tempFilePath,
        success:res=>{
          wx.showToast({
            title:'保存成功！',
          })
        },
        fail:err=>{
          wx.showToast({
            title:'保存失败',
            icon:'none'
          })
        }
      })
    },
    fail:err=>{
      console.log(err)
    }
  })
},
  /**
   * 页面的初始数据
   */
  data: {
    topic: {},
    id: '',
    openid: '',
    isLike: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


    
photos.doc(options.id).get({
  success:res=>{
    this.setData({photo:res.data})
  }
})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:'给你分享一张好看的图片',
      path:'pages/detail/detail?id='+this.data.photo._id
    }
    },
    

  })